package com.chunayev.numerology.gui.action;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextArea;


import com.chunayev.numerology.domain.PositionSummary;
import com.chunayev.numerology.util.Constants;
import com.chunayev.numerology.util.db.DBInitializer;
import com.sleepycat.je.DatabaseException;

/**
 * @author Sergey Chunayev
 */
public class SaveSummaryAction extends AbstractAction {
    
    /**
     * 
     */
    private static final long serialVersionUID = 2728729015784920739L;
    
    private JTextArea descriptionArea;
    
    private PositionSummary positionSummary;
    
    /**
     * 
     */
    public SaveSummaryAction() {
        putValue(Action.NAME, Constants.RESOURCE_BUNDLE
                .getString("action.save.summary.name"));
        putValue(Action.MNEMONIC_KEY, KeyEvent.VK_S);
    }
    
    /*
     * (non-Javadoc)
     * @see
     * java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
     */
    @Override
    public void actionPerformed(final ActionEvent e) {
        try {
            getPositionSummary().setDescription(descriptionArea.getText());
            DBInitializer.SUMMARY_PRIMARY_INDEX.put(getPositionSummary());
        } catch (final DatabaseException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    /**
     * @return the descriptionArea
     */
    public JTextArea getDescriptionArea() {
        return this.descriptionArea;
    }
    
    /**
     * @return the positionSummary
     */
    public PositionSummary getPositionSummary() {
        return this.positionSummary;
    }
    
    /**
     * @param descriptionArea
     *            the descriptionArea to set
     */
    public void setDescriptionArea(final JTextArea descriptionArea) {
        this.descriptionArea = descriptionArea;
    }
    
    /**
     * @param positionSummary
     *            the positionSummary to set
     */
    public void setPositionSummary(final PositionSummary positionSummary) {
        this.positionSummary = positionSummary;
    }
    
}
